package com.farewell.blog.controller.admin;

import com.baomidou.mybatisplus.plugins.Page;
import com.farewell.blog.entity.QueryBean;
import com.farewell.blog.entity.SearchBlog;
import com.farewell.blog.pojo.Blog;
import com.farewell.blog.service.BlogService;
import com.farewell.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author Farewell
 * @date 2021/2/8
 */
@Controller
@RequestMapping("/admin")
public class BlogsController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/blogs")
    public String blog(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        QueryBean queryBean = new QueryBean(pageNum, 10, null);
        Page<Blog> pageData = blogService.listBlog(queryBean);
        model.addAttribute("pageData", pageData);
        model.addAttribute("types", typeService.getAllTypes());
        return "admin/blogs";
    }

    @PostMapping("/blogs/search")
    public String search(SearchBlog searchBlog, Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        QueryBean queryBean = new QueryBean(pageNum, 10, null);
        Page<Blog> pageData = blogService.getBlogsBySearch(queryBean, searchBlog);
        model.addAttribute("pageData", pageData);
        return "admin/blogs :: blogList";
    }

    @PostMapping("/blogs/save")
    public String saveBlog(Blog blog, RedirectAttributes redirectAttributes) {
        try {
            if (blog.getCheckId() != null) {
                // 非空代表是修改操作
                blogService.updateBlog(blog);
                redirectAttributes.addFlashAttribute("message", "修改成功");
            }else {
                blogService.saveBlog(blog);
                redirectAttributes.addFlashAttribute("message", "添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute("message", "操作失败");
        }
        return "redirect:/admin/blogs";
    }

    @GetMapping("/blogs/input")
    public String input(Model model) {
        model.addAttribute("types", typeService.getAllTypes());
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }

    @GetMapping("/blogs/{id}/delete")
    public String deleteBlog(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        try {
            blogService.deleteBlog(id);
            redirectAttributes.addFlashAttribute("message", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute("message", "删除失败");
        }
        return "redirect:/admin/blogs";
    }

    @GetMapping("/blogs/{id}/input")
    public String input(@PathVariable("id") Long id, Model model) {
        Blog blog = blogService.getBlogById(id);
        model.addAttribute("blog", blog);
        model.addAttribute("types", typeService.getAllTypes());
        return "admin/blogs-input";
    }
}
